package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.Service.OrderService;
import com.itheima.entity.Result;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;


    //获取时间区域内到诊人数和预约人数
    @RequestMapping("/findOrderDateValue")
    public Result findOrderDateValue(String value1){
        try {
            String[] split = value1.split(",");
            //取出开始和结束时间
            Date sd = DateUtils.parseString2Date(split[0]);
            Date ed = DateUtils.parseString2Date(split[1]);
            //获取当天时间
            Date today = new Date();
            if (sd.getTime()>today.getTime() || ed.getTime()>today.getTime()){
                return new Result(false,"未来不可预测哦");
            }


            //拿到每天的日期
            ArrayList<String> dayList = DateUtils.findDaysStr(split[0], split[1]);

            Map<String, Object> map = findOrders(dayList);

            map.put("dayList", dayList);


            return new Result(true, "到诊人数和预约人数查询成功", map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, "到诊人数和预约人数查询失败");
        }
    }

    //获取本周时间区域内到诊人数和预约人数
    @RequestMapping("/findOrderDate")
    public Result findOrderDate() throws Exception {
        try {

            //使用工具类找到当前时间前一周内每天的时间日期
            ArrayList<String> dayList = DateUtils.findDaysToWeek();


            Map<String, Object> map = findOrders(dayList);

            map.put("dayList", dayList);



            return new Result(true, "到诊人数和预约人数查询成功", map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, "到诊人数和预约人数查询失败");
        }
    }



    public Map<String, Object> findOrders( ArrayList<String> dayList){
        Map<String, Object> map = new HashMap<>();

        //查询到诊数量
        List<Integer> orderStatus = orderService.findtoOrderStatus(dayList);
        map.put("orderStatus", orderStatus);

        //查询预约数量
        List<Integer> orderList = orderService.findtoOrderList(dayList);
        map.put("orderList", orderList);
        return map;
    }


}

